<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style>
    .layui-layer-btn .layui-layer-btn1 {
        border-color: #FF5722;
        background-color: #FF5722;
        color: #fff;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>免费领取</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div style="padding-bottom: 10px;">
                        <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
                    </div>
                    <table id="demo" lay-filter="demo"></table>
                    <script type="text/html" id="tian">
                        {{d.tian}} 天
                    </script>
                    <script type="text/html" id="time">
                        {{ formatDateTime(d.time) }}
                    </script>
                    <script type="text/html" id="status">
                        {{# if(d.status == 0){ }}
                        <button class="layui-btn layui-btn-xs layui-btn-primary">未开启</button>
                        {{# }else if(d.status == 1){ }}
                        <button class="layui-btn layui-btn-xs layui-bg-blue">正常</button>
                        {{# }else{ }}
                        <button class="layui-btn layui-btn-xs layui-bg-red">未开启</button>
                        {{# } }}
                    </script>
                    <script type="text/html" id="bao">
                        {{# if(d.bao == 1){ }}
                        <button class="layui-btn layui-btn-xs layui-bg-blue">开启</button>
                        {{# }else{ }}
                        <button class="layui-btn layui-btn-xs layui-bg-red">关闭</button>
                        {{# } }}
                    </script>
                    <script type="text/html" id="table-content-list">
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                        {{# if(d.status == 0){ }}
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="start">开启活动</a>
                        {{# }else if(d.bao == 1){ }}
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="stop">停止报名</a>
                        {{# } }}
                    </script>
                </div>
            </div>
        </div>
    </fieldset>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'laydate', 'table'], function(){
        var $ = layui.$,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate;

        //执行渲染
        table.render({
            elem: '#demo',
            url: "{:url('Activity/clock')}",
            method: 'post',
            cols: [[
                {field: 'name', title: '活动名称'},
                {field: 'pid', title: '打卡商品'},
                {field: 'tian', title: '打卡时间', templet: "#tian"},
                {field: 'bao', title: '报名状态', templet: "#bao"},
                {field: 'status', title: '活动状态', templet: "#status"},
                {fixed: 'right', title: '操作', align:'center', toolbar: '#table-content-list'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        //监听事件
        table.on('tool(demo)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;

            if (layEvent === 'stop'){
                layer.confirm('确定停止报名吗? 该操作无法撤销', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    var index = layer.load();
                    $.post("{:url('Activity/stop')}", {id:id}, function(e){
                        layer.close(index);
                        if (e == 1){
                            layer.msg('操作成功');
                            table.reload('demo')
                        }else{
                            layer.msg('操作失败');
                        }
                    });
                });
            } else if (layEvent === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑打卡活动'
                    ,content: '/admin/Activity/clock_operate/id/' + id
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            } else if (layEvent === 'start'){
                layer.confirm('确定开启活动吗?', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    var index = layer.load();
                    $.post("{:url('Activity/start_close')}", {id:id,value:1}, function(e){
                        layer.close(index);
                        if (e == 1){
                            layer.msg('操作成功');
                            table.reload('demo')
                        }else{
                            layer.msg('操作失败');
                        }
                    });
                });
            }
        });

        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加打卡活动'
                    ,content: "{:url('Activity/clock_operate')}"
                    ,area: ['100%', '100%']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
